/*
 * GpxConverter.java
 *
 * Copyright (C) 2005-2009 Tommi Laukkanen
 * http://www.substanceofcode.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

package mobiletrailexplorer.model;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.Vector;

import org.kxml2.io.KXmlParser;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import uns.fit.gis.Util;
import uns.fit.gis.gps.Track;

/**
 * GpxConverter writes track/place data in GPX format.
 * 
 * @author Tommi Laukkanen
 * @author Mario Sansone
 * @author Patrick Steiner
 * @modifier nlnngu
 */
public class GpxConverter {

    /** Convert trail to GPX format.
     * @param track
     * @param places 
     * @param includePlaces
     * @param includeMarkers
     * @return 
     */
    public String convert(Vector track) {

        StringBuffer gpx = new StringBuffer();
        addHeader(gpx);
        //addMetaData(track,gpx);
        // Create places
        
        // Create trail
        addTrailStart(gpx);
        int point;
        for(int i = 0; i < track.size(); i ++) {
            point = ((Integer)track.elementAt(i)).intValue();
            addPosition(Util.getLatitude((short)(point & 0xFFFF)), Util.getLongitude((point >> 16) & 0xFFFF), gpx);
        }
        addTrailEnd(gpx);

        // Finalize the GPX
        addFooter(gpx);
        return gpx.toString();
    }
    /**
     * TODO
     * 
     * @param gpx
     */
    public static void addHeader(StringBuffer gpx) {
        gpx.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
        gpx.append("<gpx xmlns=\"http://www.topografix.com/GPX/1/1\" " +
                "version=\"1.1\" creator=\"Mobile Trail Explorer\" " +
                "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
                "xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 " +
                "http://www.topografix.com/GPX/1/1/gpx.xsd\" >\r\n");
    }

    /**
     * TODO
     * 
     * @param gpx
     */
    public static void addTrailStart(StringBuffer gpx) {
        gpx.append("<trk>\r\n<trkseg>\r\n");
    }

    /**
     * TODO
     * 
     * @param gpx
     */
    public static void addTrailEnd(StringBuffer gpx) {
        gpx.append("</trkseg>\r\n</trk>\r\n");
    }

    public static void addFooter(StringBuffer gpx) {
        gpx.append("</gpx>\r\n");
    }

    /**
     * TODO
     * 
     * @return
     */
    public static void addPosition(double lat, double lon, StringBuffer gpx) {
        gpx.append("<trkpt lat=\"").append(lat).append("\" lon=\"").append(lon)
                .append("\">\r\n").append("<time>2005-11-07T12:03:31Z</time>").append("</trkpt>\r\n");
    }
}